/**
 * createRouter 这个为创建路由的方法
 * createWebHashHistory 这个就是vue2中路由的模式，
 *                      这里的是hash模式，这个还可以是createWebHistory等
 * RouteRecordRaw 这个为要添加的路由记录，也可以说是routes的ts类型
 */
import {createRouter,createWebHashHistory,RouteRecordRaw} from 'vue-router'
const routes:Array<RouteRecordRaw> = [
    {
        path: '/login',
        name: 'login',
        component: () => import('@/layout/LoginLayout.vue')
    },
    {
        path: '/layout',
        name: 'layout',
        component: () => import('@/layout/MainLayout.vue'),
        children: [
            {
                path: '/home',
                name: 'home',
                component: () => import('@/views/Home/index.vue'),
                alias: '/home',
                meta: {
                    title: 'Home页面'
                }
            },
            {
                path: '/user',
                name: 'user',
                component: () => import('@/views/UserManager/index.vue'),
                meta:{
                    title: 'UserManage'
                }
            },
            {
                path: '/api',
                name: 'api',
                component: () => import('@/views/Project/ApiManager.vue'),
            },
            {
                path: '/style',
                name: 'style',
                component: () => import("@/views/Project/StyleManeger.vue")
            },
        ]
    }
]
const router = createRouter({
    history: createWebHashHistory(),
    routes
})
// 导航守卫
router.beforeEach((to,from,next) => {
   if(to.path === '/login') return next()
   const TOKEN = window.localStorage.getItem('TOKEN')
   if(!TOKEN) return next('/login')
   next()
})
export default router
